package main

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"golang.org/x/crypto/bcrypt"
)

type User struct {
	gorm.Model
	Name   string
	Gender string
	Age    int64
}

// Player 是一个简单的用户模型
type Player struct {
	ID                uint
	Name              string
	Password          string
	EncryptedPassword string
}

// BeforeSave 是一个 GORM 钩子方法，在保存用户记录之前被调用
func (p *Player) BeforeSave(scope *gorm.Scope) (err error) {
	if pw, err := bcrypt.GenerateFromPassword([]byte(p.Password), 0); err == nil {
		scope.SetColumn("EncryptedPassword", pw)
	} else {
		return err
	}
	return nil
}

func main() {
	// 使用 GORM 连接到 MySQL 数据库
	db, err := gorm.Open("mysql", "usr1:12345@tcp(114.132.68.147:3306)/db1?charset=utf8mb4&parseTime=True&loc=Local")
	if err != nil {
		panic(err.Error())
	}
	// 关闭数据库连接
	defer db.Close()
	db.AutoMigrate(&User{})
	db.AutoMigrate(&Player{})
	//u1 := User{Name: "张三", Gender: "男", Age: 18}
	//u2 := User{Name: "李四", Gender: "女", Age: 20}
	//db.Create(&u1)
	//db.Create(&u2)
	//查询
	var user User
	db.First(&user)
	////更新
	//user.Name = "李四"
	//user.Age = 28
	//db.Debug().Save(&user)
	//db.Debug().Model(&user).Update("name", "老6")
	//db.Debug().Model(&user).Where("age = ?", 28).Update("name", "张三")
	//db.Debug().Model(&user).Updates(map[string]interface{}{"name": "王五", "age": 18, "gender": "女"})
	//db.Debug().Model(&user).Updates(User{Name: "李四", Age: 88})
	//db.Debug().Model(&user).Updates(User{Name: "", Age: 0})
	//db.Debug().Model(&user).Select("name").Updates(map[string]interface{}{"name": "王五", "age": 88})
	//db.Debug().Model(&user).Omit("name").Updates(map[string]interface{}{"name": "张三", "age": 88})
	//db.Debug().Model(&user).UpdateColumn("name", "张三")
	//db.Debug().Model(&user).UpdateColumns(User{Name: "李四", Age: 88})
	//db.Debug().Table("users").Where("id IN (?)", []int{1, 2}).Updates(map[string]interface{}{"name": "张三", "age": 33})
	//db.Debug().Model(User{}).Updates(User{Name: "李四", Age: 18, Gender: "男"})
	//rowsAffected := db.Debug().Model(User{}).Updates(User{Name: "张三", Age: 18}).RowsAffected
	//fmt.Printf("Rows affected: %d\n", rowsAffected)
	//db.Debug().Model(&user).Update("age", gorm.Expr("age + ?", 2))
	//db.Debug().Model(&user).Updates(map[string]interface{}{"age": gorm.Expr("age * ? ", 2)})
	//db.Debug().Model(&User{}).UpdateColumn("age", gorm.Expr("age / ?", 2))
	//db.Debug().Model(&user).Where("age > 10").UpdateColumn("age", gorm.Expr("age - ?", 72))
	// 创建一个新用户
	//p := Player{Name: "张三", Password: "123456"}
	//db.Create(&p)
	//
	//// 查询并打印创建的用户
	//var createdUser Player
	//db.Where("name = ?", "张三").First(&createdUser)
	//fmt.Printf("Created user: %+v\n", createdUser)
	//db.Debug().Model(&user).Set("gorm:update_option", "OPTION (OPTIMIZE FOR UNKNOWN)").Update("name", "王五")
	//db.Debug().Delete(&user)
	//u2 := User{Name: "王李四", Gender: "男", Age: 10}
	//u3 := User{Name: "李四张", Gender: "男", Age: 76}
	//u4 := User{Name: "李李四", Gender: "女", Age: 22}
	//db.Create(&u2)
	//db.Create(&u3)
	//db.Create(&u4)
	//db.Debug().Where("name LIKE ?", "%李四%").Delete(User{})
	//db.Debug().Delete(User{}, "name LIKE ?", "%张三%")
	//db.Debug().Where("age = 18").Find(&user)
	//db.Debug().Unscoped().Where("age = 18").Find(&user)
	db.Debug().Unscoped().Delete(&user)
}
